package com.soilflow.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soilflow.common.domain.LandProtocol;
import com.soilflow.common.domain.dto.LandProtocolDto;
import com.soilflow.common.domain.vo.LandProtocolVo;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.common.utils.bean.BeanUtils;
import com.soilflow.info.service.LandInfoService;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.info.mapper.LandProtocolMapper;
import com.soilflow.info.service.LandProtocolService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author dhp
 * @date 2023/4/28 - 10:48
 */
@Service
public class LandProtocolServiceImpl extends ServiceImpl<LandProtocolMapper, LandProtocol> implements LandProtocolService {

    @Resource
    private LandInfoService landInfoService;
    /* 查询【协议转让】
     *
     * @param id 【协议转让】主键
     * @return 【协议转让】
     */
    @Override
    public LandProtocolVo selectLandProtocolById(Long id) {
        LandProtocolVo landProtocolVos = this.baseMapper.selectLandProtocolById(id);
        return landProtocolVos;
    }

    /**
     * 查询【协议转让】列表
     *
     * @param dto 【协议转让】
     * @return 【协议转让】集合
     */
    @Override
    public List<LandProtocolVo> selectLandProtocolList(LandProtocolDto dto) {
        List<LandProtocolVo> landProtocolVos = this.baseMapper.selectLandProtocolList(dto);
        return landProtocolVos;
    }

    /**
     * 新增【协议转让】
     *
     * @param dto 【协议转让】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertLandProtocol(LandProtocolDto dto) {
        //协议信息校验
        LambdaQueryWrapper<LandProtocol> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.isNotEmpty(dto.getProtocolName()),LandProtocol::getProtocolName,dto.getProtocolName())
                .last("limit 1");

        if (!ObjectUtils.isEmpty(baseMapper.selectOne(query))){
            return false;
        }
        boolean save = false;
        LandProtocol landProtocol = new LandProtocol();
        BeanUtils.copyProperties(dto, landProtocol);
        save = this.save(landProtocol);
        //修改土地状态为已转让
        return save;
    }

    /**
     * 修改【协议转让】
     *
     * @param dto 【协议转让】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateLandProtocol(LandProtocolDto dto) {
        boolean save = false;
        LandProtocol landProtocol = new LandProtocol();
        BeanUtils.copyProperties(dto, landProtocol);
        //新增协议转让
        save = this.updateById(landProtocol);


        return save;
    }

    /**
     * 批量删除【协议转让】
     *
     * @param ids 需要删除的【协议转让】主键集合
     * @return 结果
     */
    @Override
    public Boolean deleteLandProtocolByIds(List<Long> ids) {
        return this.removeByIds(ids);
    }

}

